package com.itheima.common;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class ExceptionGlobalHandler {


    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R sqlExp(SQLIntegrityConstraintViolationException e){

        e.printStackTrace();

        String message = e.getMessage();
        if (message.contains("Duplicate entry")){
            String[] split = message.split(" ");
            message="用户名"+split[2]+"已被注册";
        }

        return  R.error(message);
    }
    @ExceptionHandler(CustomException.class)
    public R exceptionHandler(CustomException ex){
        log.info("业务异常:{}",ex.getMessage());
        return R.error(ex.getMessage());
    }
}
